package com.cactus.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cactus.entity.Goods;
import com.cactus.entity.GoodsOrder;
import com.cactus.entity.User;
import com.cactus.entity.UserAdd;
import com.cactus.service.GoodsService;
import com.cactus.service.UserAddServer;
import com.cactus.utils.AjaxResult;
import com.cactus.utils.DailySerialNumberGenerator;
import com.cactus.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品接口
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserAddServer userAddServer;


    @GetMapping
    public AjaxResult find() {
        List<Goods> list = goodsService.list();
        return AjaxResult.success(list);
    }

    /**
     * 商品添加
     */
    @PostMapping("/addTo")
    public AjaxResult addTo(@RequestBody Goods goods) {
        User user = SecurityUtils.getCurrentUsername();
        UserAdd userAdd = userAddServer.getAddIdByUser(user.getUid());
        goods.setSendUserId(user.getUid());
        goods.setId(IdWorker.getId() + "");
        goods.setSendAddId(userAdd.getId());
        goods.setCourierNumber(DailySerialNumberGenerator.getNextSerialNumber(goodsService.selectMaxCourierNumber()));
        goodsService.save(goods);
        return AjaxResult.success();
    }


    /**
     * 接单
     *
     * @param goodsOrder
     * @return
     */
    @PostMapping("/takeOrders")
    public AjaxResult takeOrders(@RequestBody GoodsOrder goodsOrder) {
        //骑手
        User user = SecurityUtils.getCurrentUsername();
        goodsOrder.setRiderId(user.getUid());
        goodsService.takeOrders(goodsOrder);
        return AjaxResult.success();
    }


    @PostMapping("/signed")
    public AjaxResult signed(@RequestBody GoodsOrder goodsOrder) {
        goodsService.signed(goodsOrder);
        return AjaxResult.success();
    }


}
